package com.coocaa.x.app.hall.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;

/**
 * Created by luwei on 15-6-17.
 */
public class CircleProgressView extends View {
    private int backColor = 0;
    private int preColor = 0;
    private int radius = 20;
    private int progress = 0;
    private Paint arcPaint = new Paint();
    private Paint txtPaint = new Paint();
    private static final float ARC = 3.6f;
    private int bgWidth = 10;
    private int arcWidth = 10;
    private int textSize = 25;
    private int textHeight = 0;
    private String textWait = "";
    private String textInstall = "";
    private String textUpgrade = "";
    private PROGRESS_STATE state = PROGRESS_STATE.WAITING;

    public enum PROGRESS_STATE {
        WAITING, DOWNLOADING, INSTALLING, UPGRADING
    }

    public CircleProgressView(Context context) {
        super(context);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);
        arcPaint.setAntiAlias(true);
        txtPaint.setAntiAlias(true);
        txtPaint.setColor(Color.WHITE);

    }

    public void setState(PROGRESS_STATE state) {
        this.state = state;
        postInvalidate();
    }

    public void setBackColor(int color) {
        this.backColor = color;
    }

    public void setPreColor(int color) {
        this.preColor = color;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

    public void setTextSize(int size) {
        this.textSize = size;
        txtPaint.setTextSize(textSize);
        Rect rect = new Rect();
        txtPaint.getTextBounds("100", 0, 1, rect);
        textHeight = rect.height();
    }

    public void setTextColor(int color) {
        txtPaint.setColor(color);
    }

    public void setBgWidth(int width) {
        this.bgWidth = width;
    }

    public void setArcWidth(int width) {
        this.arcWidth = width;
    }

    public void setWaitText(String txt) {
        this.textWait = txt;
    }

    public void setInstallText(String txt) {
        this.textInstall = txt;
    }

    public void setUpgradeText(String txt) {
        this.textUpgrade = txt;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        switch (state) {
            case WAITING:
                drawWaiting(canvas);
                break;
            case DOWNLOADING:
                drawDownloading(canvas);
                break;
            case INSTALLING:
                drawInstalling(canvas);
                break;
            case UPGRADING:
                drawUpgrading(canvas);
                break;
        }
        super.onDraw(canvas);
    }

    private void drawWaiting(Canvas canvas) {
        drawTextCenter(canvas, textWait, txtPaint);
    }

    private void drawInstalling(Canvas canvas) {
        drawTextCenter(canvas, textInstall, txtPaint);
    }

    private void drawUpgrading(Canvas canvas) {
        drawTextCenter(canvas, textUpgrade, txtPaint);
    }

    private void drawDownloading(Canvas canvas) {
        LogUtils.d("circle", "width " + getWidth() + " height " + getHeight());
        initBgPaint();
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        canvas.drawCircle(centerX, centerY, radius, arcPaint);
        initArcPaint();
        float arc = progress * ARC;
        canvas.drawArc(new RectF(centerX - radius,
                centerY - radius,
                centerX + radius,
                centerY + radius), -90, arc, false, arcPaint);
        String txt = progress + "%";
        drawTextCenter(canvas, txt, txtPaint);
    }

    private void drawTextCenter(Canvas canvas, String txt, Paint paint) {
        float width = txtPaint.measureText(txt);
        canvas.drawText(txt, (getWidth() - width) / 2, (getHeight() + textHeight) / 2, paint);
    }

    private void initArcPaint() {
        arcPaint.setStrokeWidth(arcWidth);
        arcPaint.setColor(preColor);
    }

    private void initBgPaint() {
        arcPaint.setStrokeWidth(bgWidth);
        arcPaint.setColor(backColor);
    }

}
